const db = wx.cloud.database()
var util = require('../../../utils/util.js')

Page({
    data: {

        newslist: {},
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        // 保存信息在本地，下次免授权登录
        let userInfo = wx.getStorageSync('userInfo')
        this.setData({
            userInfo: userInfo,
        })
        db.collection('news').orderBy('istop', 'desc').orderBy('submitdate', 'desc').get().then(res => {
                for (var index in res.data) {
                    res.data[index].time = util.formatTime(res.data[index].submitdate)
                }
                this.setData({
                    newslist: res.data
                })
            })
            .catch(err => {
                console.error(err)
            })
    },
    hadd() {
        wx.navigateTo({
            url: '../addnews/addnews'
        })
    },
    ashare(){

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.onLoad()
    },
    newsClick: function (event) {
        wx.cloud.callFunction({
            name: 'news',
            data: {
                $url: "view_inc", // 要调用的路由的路径，传入准确路径或者通配符*
                id: event.currentTarget.dataset.id
            },
            success: res => {
                //console.log(res)
            }
        })
        wx.navigateTo({
            url: '../newsdetail/newsdetail?id=' + event.currentTarget.dataset.id
        })
    },
})